package com.diegohp.umd.renamer.ui;

import com.diegohp.swing.ListComboBoxModel;
import java.util.Locale;
import javax.swing.UIManager;

/**
 *
 * @author diegohp
 */
public class LanguageSelectorJDialog extends javax.swing.JDialog {

    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LanguageSelectorJDialog.class);
    
    /**
     * Creates new form LanguageSelectorJDialog
     */
    public LanguageSelectorJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.languagesListComboBoxModel = new ListComboBoxModel<String>();        
        
        if (Locale.getDefault().getLanguage().equals("en")) {
            logger.info("Language detected: English");
            this.languagesListComboBoxModel.addElement("English");
            this.languagesListComboBoxModel.addElement("Español");
        }
        else {
            if (Locale.getDefault().getLanguage().equals("es")) {
                logger.info("Language detected: Spanish");
                this.languagesListComboBoxModel.addElement("Español");
                this.languagesListComboBoxModel.addElement("English");
            }
            else{
                logger.info("Language detected: " + Locale.getDefault().getLanguage());
                logger.info("No resources available for this language. Loading default one (English)");
                this.languagesListComboBoxModel.addElement("English");
                this.languagesListComboBoxModel.addElement("Español");
            }
        }
        
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButtonSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/diegohp/umd/renamer/ui/resources"); // NOI18N
        setTitle(bundle.getString("language_selector_title")); // NOI18N
        setAlwaysOnTop(true);
        setResizable(false);

        jLabel1.setText(bundle.getString("language_selector")); // NOI18N

        jComboBox1.setModel(this.languagesListComboBoxModel);

        jButtonSelect.setText(bundle.getString("select")); // NOI18N
        jButtonSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelectActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(15, 15, 15)
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .add(53, 53, 53)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 203, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonSelect)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(17, 17, 17)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButtonSelect))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectActionPerformed

        String selected = (String) this.languagesListComboBoxModel.getSelectedItem();
        if (selected.equals("English")) {
            Locale.setDefault(Locale.ENGLISH);
            logger.info("Language set to English");
        } else {
            Locale.setDefault(new Locale("es"));
            logger.info("Language set to Spanish");
        }
        
        this.dispose();

    }//GEN-LAST:event_jButtonSelectActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        try {
            javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LanguageSelectorJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LanguageSelectorJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LanguageSelectorJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LanguageSelectorJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        LanguageSelectorJDialog dialog = new LanguageSelectorJDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                System.exit(0);
            }
        });
        dialog.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSelect;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    private ListComboBoxModel<String> languagesListComboBoxModel;
}
